﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HtmlAgilityPack;

namespace Web
{
    public partial class WebForm1 : System.Web.UI.Page
    {
       
        protected void Page_Load(object sender, EventArgs e)
        {
            
            
        }

        protected void btnGet_Click(object sender, EventArgs e)
        {
            HtmlWeb web = new HtmlWeb();
            List<Article> Articles = new List<Article>();
            for(int i=1;i<201;i++)
            {
            HtmlDocument doc = web.Load("http://www.cnblogs.com/p"+i);           
            HtmlNode a = doc.GetElementbyId("post_list");    //id只有一个，适合使用getelementbyid方法        
            HtmlNodeCollection aa = a.SelectNodes(".//div[@class='post_item']");//取出post——list下所有类名为Post——item的div
            
            foreach(HtmlNode aaa in aa)
            {
                Article article = new Article();
                HtmlNode aaaspan = aaa.SelectSingleNode(".//div[@class='post_item_body']");//取出每一个post_item下的post——item_body
                HtmlNode title = aaaspan.SelectSingleNode(".//a[@class='titlelnk']");//取出post_item——doby下的类名为titlelnk的a
                article.ArticleTitle = title.InnerText;
                HtmlNode summary = aaa.SelectSingleNode(".//p[@class='post_item_summary']");
                article.ArticleSummary = summary.InnerText;
                HtmlNode foot = aaa.SelectSingleNode(".//div[@class='post_item_foot']");
                HtmlNode author = foot.SelectSingleNode(".//a[@class='lightblue']");
                article.Author = author.InnerText;
                HtmlNode commentall = foot.SelectSingleNode(".//span[@class='article_comment']");
                HtmlNode comment = commentall.SelectSingleNode(".//a/span");
                article.ArticleComment = Convert.ToInt32(comment.InnerText);
                
                Articles.Add(article);
            }
            }
            rtp.DataSource = Articles;
            rtp.DataBind();
           
           
        }

       
       
    }
}